package Download;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;

import Crawler_data.Crawler_data;
import each_class.SongClass;
import each_class.User;

public class DowMusic {

	private static String pathmusic;
	private static String pathimage;
	private static String pathlyric;

	public static boolean DowMusic(SongClass songClass, User us) throws Exception { // 下载或缓存音乐和图片
		//System.out.println(songClass);
		InputStream in = Crawler_data.Get_music(songClass);
		if (in == null || in.available() == 0) {
			return false;
		} else {

			if (songClass.isDownload()) {
				File file0 = new File(
						us.getUser_dowpath() + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".mp3");
				if (!file0.exists()) {
					// 生成文件夹
					File file = new File(us.getUser_dowpath());
					if (!file.exists()) { // 如果path文件夹不存在
						file.mkdirs(); // 创建文件夹
					}
					songClass.setDowpath(us.getUser_dowpath());
					pathmusic = us.getUser_dowpath() + "\\" + songClass.getSong_name() + songClass.getSong_id()
							+ ".mp3";
					pathimage = us.getUser_dowpath() + "\\" + songClass.getSong_name() + songClass.getSong_id()
							+ ".jpg";
					pathlyric = us.getUser_dowpath() + "\\" + songClass.getSong_name() + songClass.getSong_id()
							+ ".obj";
				}

			} else if (songClass.isFavorite()) {
				// 生成文件夹
				File file = new File(us.getUser_cachingpath());
				if (!file.exists()) { // 如果path文件夹不存在
					file.mkdirs(); // 创建文件夹
				}
				songClass.setCachingpath(us.getUser_cachingpath());
				pathmusic = us.getUser_cachingpath() + "\\" + songClass.getSong_name() + songClass.getSong_id()
						+ ".mp3";
				pathimage = us.getUser_cachingpath() + "\\" + songClass.getSong_name() + songClass.getSong_id()
						+ ".jpg";
				pathlyric = us.getUser_cachingpath() + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".obj";
			}
		//	System.out.println(pathmusic + "asd");
			OutputStream out = new FileOutputStream(new File(pathmusic));
			BufferedInputStream bi = new BufferedInputStream(in);
			BufferedOutputStream bo = new BufferedOutputStream(out);

			byte[] bytes = new byte[64];
			int len = 0;
			while ((len = bi.read(bytes)) != -1) {
				bo.write(bytes, 0, len);
			}
			bo.flush();
			out.flush();
			bo.close();
			out.close();

			InputStream in1 = Crawler_data.Get_image(songClass);
			if (in1 != null && in1.available() > 0) {
				OutputStream out1 = new FileOutputStream(new File(pathimage));
				BufferedInputStream bi1 = new BufferedInputStream(in1);
				BufferedOutputStream bo1 = new BufferedOutputStream(out1);

				byte[] bytes1 = new byte[1024];
				int len1;
				while ((len1 = bi1.read(bytes1)) != -1) {
					bo1.write(bytes1, 0, len1);
				}
				bo1.flush();
				out1.flush();
				bo1.close();
				out1.close();

			}

			
			
			FileOutputStream fos = new FileOutputStream(new File(pathlyric));
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(songClass.getLyric());
			oos.flush();
			oos.close();

			return true;
		}

	}
}
